<?php
// +----------------------------------------------------------------------
// |门店相关操作类
// +----------------------------------------------------------------------
// | Copyright (c) 2010 旋风工作室 All rights reserved.
// +----------------------------------------------------------------------
// | Author：XF.Zhoujz
// +----------------------------------------------------------------------

class StoreAction extends Action{
	//数据列表
	public function Index(){
		$this->validate();
		import("@.ORG.Page");
		$store = D("store");
		$count = $store->Table("suser")->join("store on suser.suser_id = store.store_mid")->count();
		$p = new Page($count,10);
		$list = $store->Table("suser")->join("store on suser.suser_id = store.store_mid")
					->limit($p->firstRow.','.$p->listRows)->order('suser.suser_id asc')->findAll();
		$p->setConfig('header','篇记录');
		$p->setConfig('prve','<');
		$p->setConfig('next','>'); 
	    $p->setConfig('first','<<'); 
	    $p->setConfig('last','>>'); 
		$page = $p->show(); 
	    $order = new OrderStore();
	    
	    $this->assign("nowpage",!empty($_GET["p"])?$_GET["p"]:1);
		$this->assign("page", $page); 
	    $this->assign("list", $list); 
	    $this->assign("order", $order); 
	    $this->display('index');

	}
	
	//用户查询
	public function Search(){
		$this->validate();
		import("@.ORG.Page");
		$storeModel = D("store");
		if(strlen(trim($_POST['suser_id']))!=0){
			$map['suser.suser_id'] = array("like","%".trim($_POST['suser_id'])."%");	
		}
		if(strlen(trim($_POST['suser_realname']))!=0){
			$map['suser.suser_realname'] = array("like","%".trim($_POST['suser_realname'])."%");
		}
		if(strlen(trim($_POST['store_name']))!=0){
			$map['store.store_name'] = array("like","%".trim($_POST['store_name'])."%");
		}
		if(strlen(trim($_POST['store_static']))!=0 && trim($_POST['store_static'] !="2")){
			$map['store.store_static'] = array("eq",trim($_POST['store_static']));
		}
		$count = $storeModel->Table("suser")->join("store on suser.suser_id = store.store_mid")->where($map)->count();
		$p = new Page($count,10);
		$list = $storeModel->Table("suser")->join("store on suser.suser_id = store.store_mid")
					->limit($p->firstRow.','.$p->listRows)->order('suser.suser_id asc')->where($map)->findAll();
		$p->setConfig('header','篇记录');
		$p->setConfig('prve','<');
		$p->setConfig('next','>'); 
	    $p->setConfig('first','<<'); 
	    $p->setConfig('last','>>'); 
	    $page = $p->show(); 
	    $this->assign("nowpage",!empty($_GET["p"])?$_GET["p"]:1);
		$this->assign("page", $page); 
	    $this->assign("list", $list);
	    $this->assign("post",$_POST?$_POST:$_GET); 
	    $this->display('index');
	}
	
	//排序
	public function Sort(){
		$this->validate();
		import("@.ORG.Page");
		$store = D("store");
		if(strlen(trim($_GET['suser_id']))!=0){
			$map['suser.suser_id'] = array("like","%".trim($_GET['suser_id'])."%");	
		}
		if(strlen(trim($_GET['suser_realname']))!=0){
			$map['suser.suser_realname'] = array("like","%".trim($_GET['suser_realname'])."%");
		}
		if(strlen(trim($_GET['store_name']))!=0){
			$map['store.store_name'] = array("like","%".trim($_GET['store_name'])."%");
		}
		if(strlen(trim($_GET['store_static']))!=0 && trim($_GET['store_static'] !="2")){
			$map['store.store_static'] = array("eq",trim($_GET['store_static']));
		}
		echo "a:".$_GET['store_static'];
		echo "b:".strlen(trim($_GET['store_static']));
		$count = $store->Table("suser")->join("store on suser.suser_id = store.store_mid")
					->order(trim($_GET["sortString"])." ".trim($_GET["order"]))->where($map)->count();
		$p = new Page($count,10);
		$list = $store->Table("suser")->join("store on suser.suser_id = store.store_mid")
					->limit($p->firstRow.','.$p->listRows)->order('suser.suser_id asc')
					->order(trim($_GET["sortString"])." ".trim($_GET["order"]))->where($map)->findAll();
		$p->setConfig('header','篇记录');
		$p->setConfig('prve','<');
		$p->setConfig('next','>'); 
	    $p->setConfig('first','<<'); 
	    $p->setConfig('last','>>');
	    $page = $p->show();
	    $order = new OrderStore();
	    $order->currentColumn=trim($_GET["sortString"]);
	    eval("\$order->".trim($_GET["sortString"])."=trim(\$_GET[\"order\"])=='desc'?'asc':'desc';");
	    $this->assign("nowpage",!empty($_GET["p"])?$_GET["p"]:1);
		$this->assign("page", $page); 
	    $this->assign("list", $list);
	    $this->assign("order", $order);
	    $this->assign("post",$_POST?$_POST:$_GET);  
	    $this->display('index');
	}
	
	//删除指定记录
	public function delete() {
		$this->validate();
		$actionName = $this->getActionName();
		$model = D($actionName);
		$id = $_REQUEST['id'];
		$pk= $model->getPk();
		if (isset ($id)) {
			$condition = array ($pk => array ('in', explode ( ',', $id ) ) );
            $result = $model->query("DELETE FROM suser WHERE `suser_id` IN (SELECT store_mid FROM store  WHERE ( `store_id` IN ('".$id."')))");
            $result = $model->query("DELETE FROM store WHERE LEFT(store_id,10) in ('".$id."')");
            $result = $model->query("DELETE FROM series WHERE series_sid in ('".$id."') or series_mid in ('".$id."')");
            
            $result = $model->query("DELETE FROM merchandise WHERE mcd_sid in ('".$id."')");
            $idarry = explode(',',$id);
            foreach($idarry as $key => $value){
                $strf  = substr($value,0,1);
				$strf .= (string)substr($value,-5);
                $result = $model->query("DELETE FROM merchandise WHERE LEFT(mcd_id,6)='".$strf."'");
            }
			if (false !== $model->where ( $condition )->delete ()) {
				$this->redirect($actionName.'/index');
			} else {
				$errmsg = $model->getDbError();
			}
		}else{
			$errmsg = $model->getError();
		}
		$this->assign('errmsg',$errmsg);
		$this->display('index');
	}
	
	//锁定指定记录
	public function lock() {
		//echo 'asd';
		//dump($_REQUEST);
		$this->validate();
		$model = D('store');
		$id = trim($_REQUEST['id_lock']);
		if (isset($id)) {
			$condition = array ('store_id' => array ('in', explode ( ',', $id ) ) );
			$map['store_static']=trim($_REQUEST['store_static_lock']);
			if (false !== $model->where ( $condition )->save($map)) {
				$this->redirect('Store/index');
			} else {
				$errmsg = $model->getDbError();
			}
		}else{
			$errmsg = $model->getError();
		}
		$this->assign('errmsg',$errmsg);
		$this->display('index');
	}
	
	//用户详细信息显示
	public function detail(){
		$this->validate();
		$suserModel = D("suser");
		$storeModel = D("stroe");
		$id = trim($_REQUEST['id']);
		$condition = array ('suser_id' => array ('in', explode ( ',', $id ) ) );
		$vo = $suserModel->where($condition)->find();
		$list = $storeModel->Table("suser")->join("store on suser.suser_id = store.store_mid")
					->where($condition)->findAll();
		$this->assign("vo",$vo);
		$this->assign("list",$list);
		$this->display();
	}
	
	//充值记录
	public function recharge(){
		$this->validate();
		import("@.ORG.Page");
		$rechargeModel = D('recharge');
		$id = trim($_REQUEST['id']);
		$count = $rechargeModel->Table("suser")->join("recharge on suser.suser_id = recharge.recharge_mid")
				->where("suser_id='".$id."'")->count();
		$p = new Page($count,10);
		$list = $rechargeModel->table("suser")->join("recharge on suser.suser_id = recharge.recharge_mid")
				->where("suser_id='".$id."'")->limit($p->firstRow.','.$p->listRows)->order('suser.suser_id asc')->select();
		$p->setConfig('header','篇记录');
		$p->setConfig('prve','<');
		$p->setConfig('next','>'); 
	    $p->setConfig('first','<<'); 
	    $p->setConfig('last','>>'); 
		$page = $p->show(); 
		$this->assign("list",$list);
		$this->assign("page", $page); 
		$this->display();
	}
	
	//登陆信息验证
	public function validate(){
		if(empty($_SESSION['admin'])){
			$this->redirect('Public/login');
		}
	}
}

//排序类
class OrderStore{	
	public $suser_id="desc";
	public $suser_realname="desc";
	public $store_id="desc";
	public $store_name="desc";
	public $suser_pointcard="desc";
	public $suser_totlepointcard="desc";
	public $store_static="desc";
	public $store_recommend="desc";
	public $currentColumn="";
}
?>